var webpack = require('webpack');
var path = require('path');
var HtmlWebpackPlugin = require('html-webpack-plugin');      //生成html文件
var cleanWebpackPlugin = require('clean-webpack-plugin');

module.exports = {
    devtool: 'cheap-module-eval-source-map',
    entry: __dirname + '/src/main.js',
    output: {
        path: __dirname + '/dist',
        filename: '[name].[hash].[js]'
    },
    devServer: {
        contentBase: './dist',  //默认webpack-dev-server会为根文件夹提供本地服务器，如果想为另外一个目录下的文件提供本地服务器，应该在这里设置其所在目录（本例设置到"dist"目录）
        historyApiFallback: true,//在开发单页应用时非常有用，它依赖于HTML5 history API，如果设置为true，所有的跳转将指向index.html
        inline: true,//设置为true，当源文件改变时会自动刷新页面
        port: 8080,//设置默认监听端口，如果省略，默认为"8080"
        hot: true
    },
    module: {
      rules: [
        {
          test: /\.jsx?$/,
          loader: 'babel-loader',
          exclude: /node_modules/
        },
        {
          test: /.css?$/,
          loader: 'style-loader!css-loader'
        },
        {
          test: /.less?$/,
          loader: 'style-loader!css-loader!less-loader'
        },
        {
            test: /\.(png|jpg|gif|woff|woff2)$/,
            use: [
                {
                    loader: 'url-loader',
                    options: {
                        limit: 10000
                    }
                }
            ]
        },
        {
            test:/\.json$/,
            loader:'json-loader'
        },
        {
            test:/\.html$/,
            loader:'html-withimg-loader'
        }
      ]
    },
    resolve: {
        extensions: ['.js', '.jsx'],
        alias: {
            Components: path.resolve(__dirname,'src/Components')
        }
    },
    plugins: [
        new webpack.HotModuleReplacementPlugin(),//热加载插件
        new HtmlWebpackPlugin({
            filename: 'index.html',
            template: __dirname + '/src/index.html'
        })
        ,
        new cleanWebpackPlugin(
            ['dist'], //匹配要删除的文件，这里则指定每次对dist文件夹进行清理
            {
                root: __dirname,//指定插件根目录位置
                verbose: true, //开启在控制台输出信息
                dry: false //启用删除文件
            }
        )
    ]
}